package com.health.ts.service.consultation;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.health.ts.dao.attachment.AttachmentMapper;
import com.health.ts.dao.consultation.AssignInfoMapper;
import com.health.ts.dao.consultation.ConsultationInfoMapper;
import com.health.ts.dao.consultation.ConsultationLogMapper;
import com.health.ts.dao.consultation.PatientInfoMapper;
import com.health.ts.dao.consultation.ReportInfoMapper;
import com.health.ts.dao.consultation.VAssignListMapper;
import com.health.ts.dao.consultation.VQueryConsulationMapper;
import com.health.ts.entity.attachment.Attachment;
import com.health.ts.entity.consultation.AssignInfo;
import com.health.ts.entity.consultation.ConsultationForm;
import com.health.ts.entity.consultation.ConsultationInfo;
import com.health.ts.entity.consultation.ConsultationLog;
import com.health.ts.entity.consultation.ConsultationResponse;
import com.health.ts.entity.consultation.PatientInfo;
import com.health.ts.entity.consultation.ReportInfo;
import com.health.ts.entity.consultation.RequestParam;
import com.health.ts.entity.consultation.ResponseParam;
import com.health.ts.entity.consultation.VAssignList;
import com.health.ts.entity.consultation.VQueryConsulation;
import com.health.ts.utils.BeanConvertUtils;
import com.health.ts.utils.GenerateIdUtil;
import com.health.ts.utils.SessionUserUtil;
/**
 * <p>
 * 会诊信息表 服务实现类
 * </p>
 * @author ${author}
 * @since 2017-06-13
 */
@Service
public class ConsultationInfoService extends ServiceImpl<ConsultationInfoMapper, ConsultationInfo>{
	
	@Autowired
	AssignInfoMapper assignInfoMapper;
	
	@Autowired
	ConsultationInfoMapper consultationInfoMapper;
	
	@Autowired
	ConsultationLogMapper consultationLogMapper;
	
	@Autowired
	ReportInfoMapper reportInfoMapper;
	
	@Autowired
	PatientInfoMapper patientInfoMapper;
	
	@Autowired
	AttachmentMapper attechmentMapper;
	
	@Autowired
	VQueryConsulationMapper vQueryConsulationMapper;
	
	@Autowired
	VAssignListMapper vAssignListMapper;
	
	private static final String FLOW_FORWARD="0";//流程前进标识
	
	private static final String FLOW_BACK="1";//流程后推标识
	
	/**
	 * 保存会诊信息
	 * 1.会诊类型 需要controller转存处理下
	 * 2.拆分成2个类进行保存
	 * 3.返回会诊id
	 * @param form
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public ResponseParam saveConsultation(ConsultationForm form){
		Date now = new Date();
		String consultationId=null;
		String patient_id=null;
		PatientInfo patientInfo=loadPatient(form);//加载患者信息
		if(StringUtils.isNotEmpty(patientInfo.getId())){//有id则更新
			patient_id=patientInfo.getId();
			patientInfo.setUpdate_time(now);
			patientInfoMapper.updateById(patientInfo);
		}else{
			patient_id=GenerateIdUtil.makeID();
			patientInfo.setId(patient_id);
			form.setPatient_id(patient_id);
			patientInfo.setCreate_time(now);
			patientInfo.setUpdate_time(now);
			patientInfoMapper.insert(patientInfo);
		}
		ConsultationInfo consultation=loadConsultation(form);//保存会诊信息
		consultation.setCon_status(ConsultationStatus.STA_APPLY);//状态变为待申请
		consultation.setApply_time(now);//设置申请时间
		consultation.setReason_id("");
		consultation.setReason("");
		if(StringUtils.isNotEmpty(consultation.getId())){//有id则更新
			consultationId=consultation.getId();
			consultationInfoMapper.updateById(consultation);
		}else{
			consultationId=GenerateIdUtil.makeID();
			consultation.setId(consultationId);
			consultationInfoMapper.insert(consultation);
		}
		ResponseParam response = new ResponseParam();
		response.setConsultationId(consultationId);
		response.setPatientId(patient_id);
		return response;
	}

	/**
	 * 提交会诊申请（返回成功与失败信息,暂时默认所有会诊都有二级质控）
	 * @param consultationId
	 * @param con_class
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public ResponseParam applyConsultation(ConsultationForm form){
		ResponseParam response=this.saveConsultation(form);
		String consultationId=response.getConsultationId();
		String patient_id=response.getPatientId();
		ResponseParam responses=new ResponseParam();
		responses.setConsultationId(consultationId);
		responses.setPatientId(patient_id);
//			Task task=new Task();
		ConsultationInfo info=handleInfo(consultationId,ConsultationStatus.STA_QC_BEFORE);//流程启动，更新会诊表,状态20
		consultationInfoMapper.updateById(info);
		QueryWrapper<ConsultationLog> logwrapper = new QueryWrapper<ConsultationLog>();
		logwrapper.eq("con_id", consultationId);
		logwrapper.eq("con_status", ConsultationStatus.STA_APPLY);
		List<ConsultationLog> list=consultationLogMapper.selectList(logwrapper);
		if(list.size()>0){
			consultationLogMapper.delete(logwrapper);
		}
		ConsultationLog log=handleLog(consultationId,ConsultationStatus.STA_APPLY,FLOW_FORWARD,form.getRemark(),null,null);//流程启动成功后，更新会诊日志表，状态更新为10 顺序1
		consultationLogMapper.insert(log);
		return response;
	}
	
	
	/**
	 * 前质控审核通过
	 * @param consultationId
	 * @param con_class
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public void qualityControlBefore(String consultationId,String remark){
		ConsultationInfo info=handleInfo(consultationId,ConsultationStatus.STA_ASSIGN);//前质控审核通过，更新会诊表,状态30
		consultationInfoMapper.updateById(info);
		if(!StringUtils.isNotEmpty(remark)){
			remark="";
		}
		QueryWrapper<ConsultationLog> logwrapper = new QueryWrapper<ConsultationLog>();
		logwrapper.eq("con_id", consultationId);
		logwrapper.eq("con_status", ConsultationStatus.STA_QC_BEFORE);
		List<ConsultationLog> list=consultationLogMapper.selectList(logwrapper);
		if(list.size()>0){
			consultationLogMapper.delete(logwrapper);
		}
		ConsultationLog log=handleLog(consultationId,ConsultationStatus.STA_QC_BEFORE,FLOW_FORWARD,remark,null,null);//前质控审核通过后，更新会诊日志表，状态更新为20
		consultationLogMapper.insert(log);
	}
	
	/**
	 * 前质控审核不通过
	 * @param consultationId
	 * @param con_class
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public void qualityControlBeforeRock(String consultationId,ConsultationInfo consultationInfo){
		consultationInfo.setId(consultationId);
		consultationInfo.setCon_status(ConsultationStatus.STA_APPLY);//前质控审核不通过，待申请状态，更新会诊表,状态10
		consultationInfoMapper.updateById(consultationInfo);
		//更新申请节点状态，写入退回原因
		QueryWrapper<ConsultationLog> logwrapper = new QueryWrapper<ConsultationLog>();
		logwrapper.eq("con_id", consultationId);
		logwrapper.eq("con_status", ConsultationStatus.STA_APPLY);
		ConsultationLog log=consultationLogMapper.selectList(logwrapper).get(0);//查询本次会诊上一次的步骤
		log=handleBackLog(log,consultationInfo.getRemark(),consultationInfo.getReason_id(),consultationInfo.getReason());//前质控退回到申请会诊，状态更新为10
		consultationLogMapper.updateById(log);
	}
	
	
	/**
	 * 二级前质控通过
	 * @param consultationId
	 * @param con_class
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public void qualityControlBefore2(String consultationId){
		ConsultationInfo info=handleInfo(consultationId,"40");//二级前质控通过，更新会诊表,状态40
		consultationInfoMapper.updateById(info);
		ConsultationLog log=handleLog(consultationId,"30",FLOW_FORWARD,null,null,null);//二级质控审核通过后，更新会诊日志表，状态更新为30
		consultationLogMapper.insert(log);
	}
	
	/**  
	* @Title: qualityControlBeforeRock2  
	* @Description: 二级前质控不通过 
	* @param @param consultationId
	* @param @param con_class
	* @param @return    参数  
	* @return String    返回类型  
	* @throws  
	*/ 
	@Transactional(rollbackFor = Exception.class)
	public void qualityControlBeforeRock2(String consultationId,ConsultationInfo info){
		info.setCon_status("20");//二级前质控不通过，回退到前质控，更新会诊表,状态20
		consultationInfoMapper.updateById(info);
		//查询
		ConsultationLog log=handleLog(consultationId,"30",FLOW_BACK,info.getRemark(),null,null);//二级前质控退回到待前质控，更新会诊日志表，状态更新为20
		consultationLogMapper.insert(log);
	}
	
	/**  
	* @Title: assignDone  
	* @Description: 分诊通过
	* @param @param consultationId
	* @param @param reqParam
	* @return String    返回类型  
	* @throws  
	*/ 
	@Transactional(rollbackFor = Exception.class)
	public void assignDone(String consultationId,RequestParam reqParam){
		//分诊通过，直接待报告状态，更新会诊表,状态40
		ConsultationInfo info=handleInfo(consultationId,ConsultationStatus.STA_REPORT);
		consultationInfoMapper.updateById(info);
		
		//删除旧的分诊信息
		QueryWrapper<AssignInfo> assignDeleteWrapper = new QueryWrapper<AssignInfo>();
		assignDeleteWrapper.eq("con_id", consultationId);
		assignInfoMapper.delete(assignDeleteWrapper);
		
		//写入分诊信息
		List<AssignInfo> list=reqParam.getAssignList();
		if(list.size()>0){
			for(AssignInfo assignInfo:list){
				assignInfo.setId(GenerateIdUtil.makeID());
				assignInfo.setCon_id(consultationId);
				assignInfoMapper.insert(assignInfo);
			}
		}
		//流程日志 ：分诊通过，记录30
		QueryWrapper<ConsultationLog> logwrapper = new QueryWrapper<ConsultationLog>();
		logwrapper.eq("con_id", consultationId);
		logwrapper.eq("con_status", ConsultationStatus.STA_ASSIGN);
		List<ConsultationLog> listlog=consultationLogMapper.selectList(logwrapper);
		if(listlog.size()>0){
			consultationLogMapper.delete(logwrapper);
		}
		ConsultationLog log=handleLog(consultationId,ConsultationStatus.STA_ASSIGN,FLOW_FORWARD,reqParam.getRemark(),null,null);
		consultationLogMapper.insert(log);
	}
	
	/**  
	* @Title: assignBack  
	* @Description: 分诊不通过
	* @param @param consultationId
	* @param @param con_class
	* @param @return    参数  
	* @return String    返回类型  
	* @throws  
	*/ 
	@Transactional(rollbackFor = Exception.class)
	public void assignBack(String consultationId,ConsultationInfo info){
		info.setId(consultationId);
		info.setCon_status(ConsultationStatus.STA_QC_BEFORE);//分诊不通过就退回前质控，待前质控状态，更新会诊表,状态20
		consultationInfoMapper.updateById(info);
		//查询状态为待前置控的进行更新
		QueryWrapper<ConsultationLog> logwrapper = new QueryWrapper<ConsultationLog>();
		logwrapper.eq("con_id", consultationId);
		logwrapper.eq("con_status", ConsultationStatus.STA_QC_BEFORE);
		ConsultationLog log=consultationLogMapper.selectList(logwrapper).get(0);//查询本次会诊上一次的步骤
		log=handleBackLog(log,info.getRemark(),info.getReason_id(),info.getReason());//会诊日志表分诊操作，分诊不通过，状态更新为30
		consultationLogMapper.updateById(log);
	}
	
	/**  
	* @Title: writeReport  
	* @Description: 写报告 
	* @param @param consultationId
	* @param @param con_type
	* @param @param reportInfo
	* @param @return    参数  
	* @return String    返回类型  
	* @throws  
	*/ 
	@Transactional(rollbackFor = Exception.class)
	public void writeReport(String consultationId,RequestParam reqParam){
		ConsultationInfo info=handleInfo(consultationId,ConsultationStatus.STA_QC_AFTER);//更新会诊状态,待后质控，状态变50
		consultationInfoMapper.updateById(info);
		//写入报告信息
		ReportInfo reportInfo=reqParam.getReportInfo();
		//根据会诊id查询是否有报告，有则修改，没有则新增，必须修改的
		String report_id=reportInfo.getId();
		if(StringUtils.isNotEmpty(report_id)){//更新
			reportInfo.setCon_id(consultationId);
			handleReportInfo(reportInfo);
			reportInfoMapper.updateById(reportInfo);
		}else{//新增
			reportInfo.setId(GenerateIdUtil.makeID());
			reportInfo.setCon_id(consultationId);
			handleReportInfo(reportInfo);
			reportInfoMapper.insert(reportInfo);
		}
		
		//会诊流程日志：更新日志，将已完成的报告写入，日志状态变成50,记录前进
		QueryWrapper<ConsultationLog> logwrapper = new QueryWrapper<ConsultationLog>();
		logwrapper.eq("con_id", consultationId);
		logwrapper.eq("con_status", ConsultationStatus.STA_REPORT);
		List<ConsultationLog> listlog=consultationLogMapper.selectList(logwrapper);
		if(listlog.size()>0){
			consultationLogMapper.delete(logwrapper);
		}
		ConsultationLog log=handleLog(consultationId,ConsultationStatus.STA_REPORT,FLOW_FORWARD,reqParam.getRemark(),null,null);
		consultationLogMapper.insert(log);
	}
	
	/**  
	* @Title: writeReportBack  
	* @Description: 报告退回，日志更新上一次状态码 -10，
	* @param @param consultationId
	* @param @return    参数  
	* @return String    返回类型  
	* @throws  
	*/ 
	@Transactional(rollbackFor = Exception.class)
	public void writeReportBack(String consultationId,ConsultationInfo info){
		info.setId(consultationId);
		info.setCon_status(ConsultationStatus.STA_ASSIGN);//报告不通过，待分诊，更新会诊表,状态30
		consultationInfoMapper.updateById(info);
		//查询状态为待分诊的，更新原因
		QueryWrapper<ConsultationLog> logwrapper = new QueryWrapper<ConsultationLog>();
		logwrapper.eq("con_id", consultationId);
		logwrapper.eq("con_status", ConsultationStatus.STA_ASSIGN);
		ConsultationLog log=consultationLogMapper.selectList(logwrapper).get(0);//查询本次会诊上一次的步骤
		log=handleBackLog(log,info.getRemark(),info.getReason_id(),info.getReason());//审核不通过，日志状态更新为40，记录为退回
		consultationLogMapper.updateById(log);
	}
	
	/**  
	* @Title: qualityControlAfter  
	* @Description: 后质控审核通过 
	* @param @param consultationId
	* @param @param con_class
	* @param @return    参数  
	* @return String    返回类型  
	* @throws  
	*/ 
	@Transactional(rollbackFor = Exception.class)
	public void qualityControlAfter(String consultationId,String remark){
		ConsultationInfo info=handleInfo(consultationId,ConsultationStatus.STA_END);//后质控通过，流程结束,状态改成end
		consultationInfoMapper.updateById(info);
		if(!StringUtils.isNotEmpty(remark)){
			remark="";
		}
		ConsultationLog log=handleLog(consultationId,ConsultationStatus.STA_QC_AFTER,FLOW_FORWARD,remark,null,null);//后质控审核通过后，更新会诊日志表，状态更新为60,实际流程日志记录结束
		consultationLogMapper.insert(log);
	}
	
	/**  
	* @Title: qualityControlAfterRock  
	* @Description: 后质控审核不通过
	* @param @param consultationId
	* @param @param con_class
	* @param @return 参数  
	* @return String 返回类型  
	* @throws  
	*/ 
	@Transactional(rollbackFor = Exception.class)
	public void qualityControlAfterRock(String consultationId,ConsultationInfo info){
		info.setId(consultationId);
		info.setCon_status(ConsultationStatus.STA_REPORT);//审核不通过退回到报告，待报告状态，更新会诊表,状态40
		consultationInfoMapper.updateById(info);
		QueryWrapper<ConsultationLog> logwrapper = new QueryWrapper<ConsultationLog>();
		logwrapper.eq("con_id", consultationId);
		logwrapper.eq("con_status", ConsultationStatus.STA_REPORT);
		ConsultationLog log=consultationLogMapper.selectList(logwrapper).get(0);//查询本次会诊上一次的步骤
		log=handleBackLog(log,info.getRemark(),info.getReason_id(),info.getReason());//审核不通过，日志状态更新为60，记录为退回
		consultationLogMapper.updateById(log);
	}
	
	/**  
	* @Title: cancelApply  
	* @Description: 取消会诊申请
	* @param @param consultationId
	* @return String 返回类型  
	* @throws  
	*/ 
	@Transactional(rollbackFor = Exception.class)
	public void cancelApply(String consultationId){
		//设置会诊信息不可用
		ConsultationInfo info=new ConsultationInfo();
		info.setId(consultationId);
		info.setValid_status("0");
		consultationInfoMapper.updateById(info);
		
		//删除会诊日志信息
//		Wrapper<ConsultationLog> deleteLogWrapper = new EntityWrapper<ConsultationLog>();
//		deleteLogWrapper.eq("con_id", consultationId);
//		consultationLogMapper.delete(deleteLogWrapper);
		
		//删除会诊附件
//		Wrapper<Attechment> deleteAttachWrapper = new EntityWrapper<Attechment>();
//		deleteAttachWrapper.eq("con_id", consultationId);
//		attechmentMapper.delete(deleteAttachWrapper);
		
		//删除分诊信息
//		Wrapper<AssignInfo> deleteAssignWrapper = new EntityWrapper<AssignInfo>();
//		deleteAssignWrapper.eq("con_id", consultationId);
//		assignInfoMapper.delete(deleteAssignWrapper);
		
		//删除报告信息
//		Wrapper<ReportInfo> deleteReportWrapper = new EntityWrapper<ReportInfo>();
//		deleteReportWrapper.eq("con_id", consultationId);
//		reportInfoMapper.delete(deleteReportWrapper);
		
	}
	
	/**
	 * 根据会诊id查询详细信息(会诊/患者/报告/费用/附件),费用后加
	 * @param consultationId
	 * @return
	 */
	public ConsultationResponse queryInfo(String consultationId){
		ConsultationInfo consultationInfo=null;
		VQueryConsulation vQueryConsulationInfo=null;
		PatientInfo patientInfo=null;
		List<ReportInfo> reportList=null;
		List<Attachment> attachList=null;
		List<VAssignList> assignList=null;
		
		if (StringUtils.isNotEmpty(consultationId)){
			//会诊信息
			consultationInfo=consultationInfoMapper.selectById(consultationId);
			//患者信息
			if(StringUtils.isNotEmpty(consultationInfo.getPatient_id())){
				patientInfo=patientInfoMapper.selectById(consultationInfo.getPatient_id());
			}
			
			//附件列表
			QueryWrapper<Attachment> attachWrapper = new QueryWrapper<Attachment>();
			attachWrapper.eq("business_id", consultationId);
			attachList=attechmentMapper.selectList(attachWrapper);
			
			//报告列表
			QueryWrapper<ReportInfo> reportWrapper = new QueryWrapper<ReportInfo>();
			reportWrapper.eq("con_id", consultationId);
			reportList=reportInfoMapper.selectList(reportWrapper);
			
			//视图信息
			vQueryConsulationInfo=vQueryConsulationMapper.selectById(consultationId);
			
			//分诊信息
			QueryWrapper<VAssignList> assignWrapper = new QueryWrapper<VAssignList>();
			assignWrapper.eq("con_id", consultationId);
			assignList = vAssignListMapper.selectList(assignWrapper);
		}
		ConsultationResponse response=new ConsultationResponse();
		response.setConsultationInfo(consultationInfo);
		response.setVconsultationInfo(vQueryConsulationInfo);
		response.setPatientInfo(patientInfo);
		response.setReportInfo(reportList);
		response.setAttachmentlist(attachList);
		response.setAssignInfo(assignList);
		String timeline=this.queryTimeAxis(consultationId);
		response.setTimeline(timeline);
		return response;
	}
	
	//查询时间轴
	private String queryTimeAxis(String consultationId){
		QueryWrapper<ConsultationLog> wrapper = new QueryWrapper<ConsultationLog>();
		if (StringUtils.isNotEmpty(consultationId)){
			wrapper.eq("con_id", consultationId); 
		}
		wrapper.orderByAsc("opr_time");//时间升序
		List<ConsultationLog> list=consultationLogMapper.selectList(wrapper);
		int size=list.size();
		StringBuffer returnStr=new StringBuffer();
		boolean back=false;
		for(int i=1;i<=size;i++){
			ConsultationLog cl=list.get(i-1);
			if(cl.getFlowflag().equals("1")){
				back=true;//标记为退回流程
				returnStr.append("<li class=\"normal no_pass\"><div class=\"line\"><div class=\"line_in\"></div>");
			}else{
				returnStr.append("<li class=\"normal pass\"><div class=\"line\"><div class=\"line_in\"></div>");
			}
			returnStr.append("<div class=\"time_node\"><div class=\"activity_name\">");
			if(cl.getCon_status().equals("10")){
				returnStr.append("申请");
			}else if(cl.getCon_status().equals("20")){
				returnStr.append("前置控");
			}else if(cl.getCon_status().equals("30")){
				returnStr.append("分配");
			}else if(cl.getCon_status().equals("40")){
				returnStr.append("报告");
			}else if(cl.getCon_status().equals("50")){
				returnStr.append("后质控");
			}
			returnStr.append("</div>");
			returnStr.append(" <div class=\"activity_Round\"></div>");
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String dateString = formatter.format(cl.getOpr_time());
			returnStr.append(" <div class=\"activity_info\"><p>"+dateString+"</p> <p>"+cl.getOperator_name()+"</p></div>");
			returnStr.append("</div></div></li>");
		}
		for(int i=size+1;i<=5;i++){//未处理节点
			if(i==(size+1)&&back==false){//待处理节点
				returnStr.append("<li class=\"normal  first\"><div class=\"line\"><div class=\"line_in\"></div>");
			}else{
				returnStr.append("<li class=\"normal\"><div class=\"line\"><div class=\"line_in\"></div>");
			}
			returnStr.append("<div class=\"time_node\"><div class=\"activity_name\">");
			if(i==1){
				returnStr.append("申请");
			}else if(i==2){
				returnStr.append("前置控");
			}else if(i==3){
				returnStr.append("分配");
			}else if(i==4){
				returnStr.append("报告");
			}else if(i==5){
				returnStr.append("后质控");
			}
			returnStr.append("</div>");
			returnStr.append(" <div class=\"activity_Round\"></div>");
			returnStr.append(" <div class=\"activity_info\"></div>");
			returnStr.append("</div></div></li>");
		}
		return returnStr.toString();
	}

	/**  
	* @Title: handleInfo  
	* @Description: 会展状态更新 
	* @param @param consultationId
	* @param @param status
	* @param @return    参数  
	* @return ConsultationInfo    返回类型  
	* @throws  
	*/ 
	private static ConsultationInfo handleInfo(String consultationId,String status){
		ConsultationInfo info=new ConsultationInfo();
		info.setReason_id("");
		info.setReason("");
		info.setId(consultationId);
		info.setCon_status(status);
		return info;
	}
	
	/**  
	* @Title: handleReportInfo  
	* @Description: 补充会诊报告信息 
	* @param @param info
	* @throws  
	*/ 
	private static void handleReportInfo(ReportInfo info){
		info.setRptdocid(SessionUserUtil.getUserId());
		info.setRptdoc_name(SessionUserUtil.getUserName());
		info.setCreate_time(new Date());
	}
	

	/**  
	* @Title: handleLog  
	* @Description: 会诊流程日志公共方法
	* @param @param consultationId
	* @param @param status
	* @param @param flow_flag
	* @param @return    参数  
	* @return ConsultationLog    返回类型  
	* @throws  
	*/ 
	private static ConsultationLog handleLog(String consultationId,String status,String flow_flag,String remark,String reasonid,String reason){
		ConsultationLog log=new ConsultationLog();
		log.setId(GenerateIdUtil.makeID());
		log.setCon_id(consultationId);
		log.setCon_status(status);
		log.setFlowflag(flow_flag);
		log.setOpr_time(new Date());
		log.setRemark(remark);
		log.setOperator_id(SessionUserUtil.getUserId());
		log.setOperator_name(SessionUserUtil.getUserName());
		log.setReason_id(reasonid);
		log.setReason(reason);
		return log;
	}
	
	
	private static ConsultationLog handleBackLog(ConsultationLog log,String remark,String reasonid,String reason){
		log.setFlowflag(FLOW_BACK);
		log.setOpr_time(new Date());
		log.setRemark(remark);
		log.setOperator_id(SessionUserUtil.getUserId());
		log.setOperator_name(SessionUserUtil.getUserName());
		log.setReason_id(reasonid);
		log.setReason(reason);
		return log;
	}

	/**  
	* @Title: loadPatient  
	* @Description:加载患者信息  
	* @param @param form
	* @param @return    参数  
	* @return PatientInfo    返回类型  
	* @throws  
	*/ 
	private static PatientInfo loadPatient(ConsultationForm form){
		return BeanConvertUtils.convertTo(form, PatientInfo::new, (s, t) -> t.setId(s.getPatient_id()));
	}
	
	/**  
	* @Title: loadConsultation  
	* @Description: 加载会诊信息  
	* @param @param form
	* @param @return    参数  
	* @return ConsultationInfo    返回类型  
	* @throws  
	*/ 
	private static ConsultationInfo loadConsultation(ConsultationForm form){
		return BeanConvertUtils.convertTo(form, ConsultationInfo::new, (s, t) ->{
			t.setId(s.getCon_id());
			t.setApply_hid(SessionUserUtil.getUserHosId());
			t.setApply_did(SessionUserUtil.getUserDeptId());
			t.setApply_docid(SessionUserUtil.getUserId());
			t.setValid_status("1");
		});
	}
}
